package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService iRoleService;

    //处理查询所有员工请求
    @RequestMapping("/list")
    // 把查询的方法的封装参数的形参改成 EmployeeQueryObject 类型，并贴上 @ModelAttribute("qo")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageResult<Employee> pageResult = employeeService.query(qo);
        model.addAttribute("pageResult", pageResult);
        List<Department> departments = departmentService.listAll();
        // 查询所有部门往模型存入，提供数据，让员工列表页面显示出来以供用户选择
        model.addAttribute("departments", departments);
        // 找视图 /WEB-INF/views/employee/list.jsp
        return "employee/list";
    }

    //处理删除请求
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
        //让浏览器重新发一次请求
    }

    //去新增或去修改页面
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        List<Department> departments = departmentService.listAll();
        // 查询所有部门往模型存入，提供数据，让员工新增修改时显示出来以供用户选择
        model.addAttribute("departments", departments);
        List<Role> roles = iRoleService.listAll();
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);

        }
        return "employee/input";
        //让浏览器重新发一次请求
    }

    //新增或修改
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() == null) {//新增
            employeeService.save(employee, ids);
        } else {
            employeeService.update(employee, ids);//修改
        }
        return "redirect:/employee/list.do";
        //让浏览器重新发一次请求
    }

    @RequestMapping("/batchDelete")
    public String batchDelete(String empList) {
        String[] str = empList.split(",");
        ArrayList<Integer> ids = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            ids.add(Integer.parseInt(str[i]));


        }
        employeeService.batchDelete(ids);
        return "redirect:/employee/list.do";

    }
    @RequestMapping("/updateIndex")
    public String updateIndex(){
        return "employee/changePassword";
    }



    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword, HttpSession session) {
        JsonResult jsonResult = new JsonResult();
        try {
            employeeService.updatePwd(oldPassword,newPassword);
            //session.invalidate();
            jsonResult.setSuccess(true);
            jsonResult.setMsg("已经成功修改密码");
        }catch (Exception e){
        e.printStackTrace();
        jsonResult.setSuccess(false);
        jsonResult.setMsg("修改密码失败");


    }
        return jsonResult;

    }

}











